/**
 * Copyright 2018 人人开源 http://www.renren.io
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package io.renren.modules.sys.controller;

import io.renren.common.exception.RRException;
import io.renren.common.utils.Constant;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.sys.entity.SysDeptEntity;
import io.renren.modules.sys.entity.SysRoleEntity;
import io.renren.modules.sys.service.SysDeptService;
import io.renren.modules.sys.service.SysRoleDeptService;
import io.renren.modules.sys.service.SysRoleService;
import io.renren.modules.sys.service.SysUserRoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;


/**
 * 部门管理
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-06-20 15:23:47
 */
@RestController
@RequestMapping("/sys/dept")
public class SysDeptController extends AbstractController {
    @Autowired
    private SysDeptService sysDeptService;


    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:dept:list")
    public R list() {
        List<SysDeptEntity> deptList = sysDeptService.queryList(new HashMap<>(16));
        return R.ok().put("deptList", deptList);
    }

    /**
     * 选择部门(添加、修改菜单)
     */
    @RequestMapping("/select")
    @RequiresPermissions("sys:dept:select")
    public R select() {
        List<SysDeptEntity> deptList = sysDeptService.queryList(new HashMap<>(16));

        //添加一级部门
        if (getUserId() == Constant.SUPER_ADMIN) {
            deptList.add(setFirstParentEntity());
        } else {
            long parentId = getCurrentUserParentId();
            if (parentId == 0) {
                deptList.add(setFirstParentEntity());
            } else {
                SysDeptEntity entity = sysDeptService.selectById(parentId);
                if (entity != null) {
                    deptList.add(entity);
                }
            }
        }
        return R.ok().put("deptList", deptList);
    }

    /**
     * 创建一级节点
     *
     * @return
     */
    private SysDeptEntity setFirstParentEntity() {
        SysDeptEntity root = new SysDeptEntity();
        root.setDeptId(0L);
        root.setName("一级部门");
        root.setParentId(-1L);
        root.setOpen(true);
        return root;
    }

    /**
     * 上级部门Id(管理员则为0)
     */
    @RequestMapping("/info")
    @RequiresPermissions("sys:dept:list")
    public R info() {
        return R.ok().put("deptId", getCurrentUserParentId());
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{deptId}")
    @RequiresPermissions("sys:dept:info")
    public R info(@PathVariable("deptId") Long deptId) {
        slelectByValidateUserParentId(deptId);

        SysDeptEntity dept = sysDeptService.selectById(deptId);
        return R.ok().put("dept", dept);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:dept:save")
    public R save(@RequestBody SysDeptEntity dept) {
        //表单校验
        ValidatorUtils.validateEntity(dept, AddGroup.class);
        long parentId = getCurrentUserParentId();
        if (dept.getParentId() <= parentId) {
            throw new RRException("无权限新增与当前部门相同等级的部门，请联系上级部门人员新增");
        }
        sysDeptService.insert(dept);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:dept:update")
    public R update(@RequestBody SysDeptEntity dept) {
        //表单校验
        ValidatorUtils.validateEntity(dept, UpdateGroup.class);
        validateParentId(dept);

        sysDeptService.updateById(dept);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete/{deptId}")
    @RequiresPermissions("sys:dept:delete")
    public R delete(@PathVariable("deptId") Long deptId) {
        slelectByValidateUserParentId(deptId);
        //判断是否有子部门
        List<Long> deptList = sysDeptService.queryDetpIdList(deptId);
        if (deptList.size() > 0) {
            return R.error("请先删除子部门");
        }
        sysDeptService.deleteById(deptId);
        return R.ok();
    }


    /**
     * 校验部门数据父级节点与当前节点是否相同
     *
     * @param dept
     */
    private void validateParentId(SysDeptEntity dept) {
        //数据相同 会死循环内存溢出。抛异常保存失败
        if (dept.getParentId().equals(dept.getDeptId())) {
            throw new RRException("父级部门不能与当前部门相同！");
        }
        if (dept.getParentId() > dept.getDeptId()) {
            throw new RRException("父级部门等级不能小于当前部门！");
        }
    }

    /**
     * 获取当前用户的父级部门id
     *
     * @return
     */
    public long getCurrentUserParentId() {
        long deptId = 0;
        if (getUserId() != Constant.SUPER_ADMIN) {
            List<SysDeptEntity> deptList = sysDeptService.queryList(new HashMap<>(16));
            Long parentId = null;
            for (SysDeptEntity sysDeptEntity : deptList) {
                if (parentId == null) {
                    parentId = sysDeptEntity.getParentId();
                    continue;
                }

                if (parentId > sysDeptEntity.getParentId().longValue()) {
                    parentId = sysDeptEntity.getParentId();
                }
            }
            deptId = parentId;
        }
        return deptId;
    }

    private void slelectByValidateUserParentId(Long deptId) {
        if (null == deptId) {
            throw new RRException("无法获取部门信息");
        }
        long parentId = getCurrentUserParentId();
        if (deptId <= parentId) {
            throw new RRException("无法获取父级部门信息");
        }
    }

}
